/*
 * 验证 value 的值是否为 null 或者 ""
 * 如果是，则在页面中标签为 id 的地方显示 info
 */
function showRequired(value, id, info) {
    var displayer = document.getElementById(id);
    if (value == null || value == "") {
        displayer.innerHTML = info;
        return false;
    } else {
        return true;
    }
}

/*
 * 隐藏必填项的提示信息
 * 如果必填项的值不为 null 或者 ""，则将页面中标签为 id 的地方清空
 */
function hideRequired(value, id) {
    var displayer = document.getElementById(id);
    if (value != null && value != "") {
        displayer.innerHTML = "";
        return true;
    } else {
        return false;
    }
}

/*
 * 验证登录的表单
 * 如果没有填写必填项，则会给出提示信息
 */
function validateLoginForm() {
    var valid = true;
    var userName = document.getElementById("LoginUserName");
    var password = document.getElementById("LoginPassword");
    if (showRequired(userName.value, "LoginUserNameRequired", "用户名是必填项") == false) {
        userName.focus();
        valid = false;
    }
    if (showRequired(password.value, "LoginPasswordRequired", "密码是必填项") == false) {
        if (valid) {
            password.focus();
            valid = false;
        }
    }
    return valid;
}

/*
 * 验证输入的是否是数字，并允许其他控制键
 * 如果不是数字，则取消当前摁下的键
 */
function onlyNumber() {
    var valid = true;
    var b1 = window.event.keyCode >= 48 && window.event.keyCode <= 57;   //0-9
    var b2 = window.event.keyCode >= 96 && window.event.keyCode <= 105;  //小键盘上的数字键
    var b3 = window.event.keyCode == 8;                                  //Backspace
    var b4 = window.event.keyCode == 9;                                  //Tab
    var b5 = window.event.keyCode == 13;                                 //Enter
    var b6 = window.event.keyCode == 16;                                 //Shift
    var b7 = window.event.keyCode == 17;                                 //Ctrl
    var b8 = window.event.keyCode == 18;                                 //Alt
    var b9 = window.event.keyCode >= 35 && window.event.keyCode <= 40;   //Home，End，方向键左、上、右、下
    var b10 = window.event.keyCode >= 45 && window.event.keyCode <= 46;  //Insert，Delete
    if(!(b1 || b2 || b3 || b4 || b5 || b6 || b7 || b8 || b9 || b10)) {// || ((b1 || b2) && window.event.shiftKey) {
        event.returnValue = false;
        valid = false;
    }
    return valid;
}

/*
 * 验证部门信息填写的表单
 * 如果没有填写必填项，则会给出提示信息
 */
function validateDeptForm() {
    var valid = true;
    var deptName = document.getElementById("DeptName");
    var capacity = document.getElementById("DeptCapacity");
    if (showRequired(deptName.value, "DeptNameRequired", "部门名称是必填项") == false) {
        deptName.focus();
        valid = false;
    }
    if (showRequired(capacity.value, "DeptCapacityRequired", "部门人数是必填项") == false) {
        if (valid) {
            capacity.focus();
            valid = false;
        }
    }
    return valid;
}

/*
 * 验证输入的是否是小数（数字和小数点），并允许其他控制键
 * 如果不是数字，则取消当前摁下的键
 */
function onlyFloat(id) {
    var valid = true;
    var b1 = window.event.keyCode >= 48 && window.event.keyCode <= 57;   //0-9
    var b2 = window.event.keyCode >= 96 && window.event.keyCode <= 105;  //小键盘上的数字键
    var b3 = window.event.keyCode == 8;                                  //Backspace
    var b4 = window.event.keyCode == 9;                                  //Tab
    var b5 = window.event.keyCode == 13;                                 //Enter
    var b6 = window.event.keyCode == 16;                                 //Shift
    var b7 = window.event.keyCode == 17;                                 //Ctrl
    var b8 = window.event.keyCode == 18;                                 //Alt
    var b9 = window.event.keyCode >= 35 && window.event.keyCode <= 40;   //Home，End，方向键左、上、右、下
    var b10 = window.event.keyCode >= 45 && window.event.keyCode <= 46;  //Insert，Delete
    var b11 = window.event.keyCode == 190;                               //小数点.
    var b12 = window.event.keyCode == 110;                               //小键盘的.
    if (!(b1 || b2 || b3 || b4 || b5 || b6 || b7 || b8 || b9 || b10 || b11 || b12) || ((b12 || b12) && window.event.shiftKey)) {
        window.event.returnValue = false;
        valid = false;
    } else {
        var value = document.getElementById(id).value;
        if ((b11 || b12) && -1 != value.indexOf(".")) {
            window.event.returnValue = false;
            valid = false;
        }
    }
    return valid;
}

/*
 * (未用）
 * 只能输入数字和至多一个小数点
 */
function clearNoNum(e) {
    //先把非数字的都替换掉，除了数字和.
    e.value = e.value.replace(/[^\d.]/g, "");
    //必须保证第一个为数字而不是.
    e.value = e.value.replace(/^\./g, "");
    //保证只有出现一个.而没有多个.
    e.value = e.value.replace(/\.{2,}/g, ".");
    //保证.只出现一次，而不能出现两次以上
    e.value = e.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
}

/*
 * （未用）
 * 去掉最后一个小数点
 */
function clearLastDot(e) {
    e.value = e.value.replace(".", "");
}

/*
 * 验证职位信息填写的表单
 * 如果没有填写必填项，则会给出提示信息
 */
function validatePositionForm() {
    var valid = true;
    var positionName = document.getElementById("PositionName");
    if (showRequired(positionName.value, "PositionNameRequired", "职位名称是必填项") == false) {
        positionName.focus();
        valid = false;
    }
    return valid;
}

/*
 * 验证职称信息填写的表单
 * 如果没有填写必填项，则会给出提示信息
 */
function validateTitleForm() {
    var valid = true;
    var titleName = document.getElementById("TitleName");
    if (showRequired(titleName.value, "TitleNameRequired", "职称名称是必填项") == false) {
        titleName.focus();
        valid = false;
    }
    return valid;
}

/*
 * 验证薪酬标准信息填写的表单
 * 如果没有填写必填项，则会给出提示信息
 */
function validateSalarystandardForm() {
    var valid = true;
    var salarystandardName = document.getElementById("SalarystandardName");
    var salarystandardSalary = document.getElementById("SalarystandardSalary");
    if (showRequired(salarystandardName.value, "SalarystandardNameRequired", "薪酬标准名称是必填项") == false) {
        salarystandardName.focus();
        valid = false;
    }
    if (showRequired(salarystandardSalary.value, "SalarystandardSalaryRequired", "薪酬量是必填项") == false) {
        if (valid) {
            salarystandardSalary.focus();
            valid = false;
        }
    }
    return valid;
}